package com.litong.litongjiaoyu.controller;

import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.entity.StudentUserinfo;
import com.litong.litongjiaoyu.service.ClassScheduleService;
import com.litong.litongjiaoyu.service.StudentService;
import com.litong.litongjiaoyu.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName StudentController
 * @Description 与学生相关的控制层
 * @Author jingz
 * @Date 2019/11/5 18:38
 * @Version 1.0
 **/

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassScheduleService scheduleService;


    /**
     * 家长端获取学生信息
     * @param openid
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/save")
    public JSONResult save(@RequestBody String openid) {
        return JSONResult.ok(studentService.selectAll(openid));
    }

    /**
     * 检查该用户是否存在，不存在则添加
     * @param studentUserinfo
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/detection")
    public JSONResult Detection(@RequestBody StudentUserinfo studentUserinfo){

        if (studentService.selectAll(studentUserinfo.getOpenid()) == null) {

            if(studentService.insert(studentUserinfo) > 0) {
                StudentUserinfo student = studentService.selectAll(studentUserinfo.getOpenid());

                return JSONResult.ok(student);
            } else {
                return JSONResult.errorMsg("fail");
            }

        } else {
            return JSONResult.ok(studentUserinfo);
        }
    }


    /**
     * 获取所有学生列表
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @GetMapping("/query")
    public JSONResult query(){
        List<StudentUserinfo> studentUserinfos = studentService.query();

        return JSONResult.ok(studentUserinfos);
    }

    @IgnoreAuth
    @ResponseBody
    @PostMapping("/insert")
    public JSONResult insert(@RequestBody JSONObject jsonObject){

        StudentUserinfo studentUserinfo = jsonObject.getObject("student", StudentUserinfo.class);

        if (studentService.insert(studentUserinfo) == 1) {
            return JSONResult.ok("success");
        } else {
            return  JSONResult.errorMsg("fail");
        }
    }

    /**
     * 根据id查询相关学生信息
     * @param id 数据库中的序号
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/selectbyid")
    public JSONResult selectById(int id){
        StudentUserinfo studentUserinfo = studentService.selectById(id);
        return JSONResult.ok(studentUserinfo);
    }

    /**
     * 登录时根据获取到的openid执行相关操作
     * @param openid 微信小程序用户的唯一标识符
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/selectall")
    public JSONResult selectAll(String openid){
        StudentUserinfo studentUserinfo = studentService.selectAll(openid);

        return JSONResult.ok(studentUserinfo);
    }


    /**
     * 更新学生信息
     * @param studentUserinfo 要更新的内容
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/update")
    public JSONResult update(@RequestBody StudentUserinfo studentUserinfo) {
        if (studentService.update(studentUserinfo) > 0) {
            return JSONResult.ok("success");
        } else {
            return  JSONResult.errorMsg("fail");
        }
    }

    /**
     * 单个删除学生信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    public  JSONResult remove(int id) {

        if (studentService.remove(id) == 1) {
            return JSONResult.ok("success");
        } else {
            return  JSONResult.errorMsg("fail");
        }
    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/batchremove")
    public JSONResult batchRemove(String[] id) {
        int[] ids = Arrays.asList(id).stream().mapToInt(Integer::parseInt).toArray();

        int len = ids.length;

        String s = "";

        int a = studentService.batchRemove(ids);

        if (a > 0) {
            if (a == len){
                s = String.format("有%d删除成功，有%d为删除成功。", a, len - a);
                return JSONResult.ok(s);
            } else {
                s = "删除成功！";
                return JSONResult.ok(s);
            }

        } else {
            s = "删除失败！";
            return JSONResult.errorMsg(s);
        }
    }

    @IgnoreAuth
    @ResponseBody
    @PostMapping("/getclassschedule")
    public JSONResult getClassSchedule(@RequestBody JSONObject jsonObject){
        return JSONResult.ok(scheduleService.GetByClassName(jsonObject.getString("classname"), jsonObject.getString("campus")));
    }
}
